package com.wude.guild.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.Generalize;
import com.wude.bean.ShareInfo;
import com.wude.guild.LoginActivity;
import com.wude.guild.R;
import com.wude.guild.dialog.SharedDialog;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.xutils.x;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wude01 on 2017/10/23.
 */
@SuppressLint("HandlerLeak")
public class GeneralizeHolder extends BaseHolder<Generalize> {
    @BindView(R.id.ivGameIcon)
    ImageView ivGameIcon;//游戏图
    @BindView(R.id.tvGameName)
    TextView tvGameName;//游戏名字
    @BindView(R.id.tv_abstract)
    TextView tv_abstract;//游戏简介
    private Activity activity;

    private Generalize currItem;

    public GeneralizeHolder(Activity activity) {
        this.activity = activity;
    }

    @Override
    protected void refreshView(Generalize generalize, int position, Activity activity) {
        setData(generalize);
    }

    @Override
    protected View initView() {
        View inflate = LinearLayout.inflate(x.app(), R.layout.generalize_holder, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        return inflate;
    }

    private void setData(Generalize data) {
        currItem = data;
        MCUtils.fillImage(ivGameIcon, data.icon);
        tvGameName.setText(data.game_name);
        tv_abstract.setText(data.features);
    }

    @OnClick({R.id.iv_shared})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_shared://分享
                if (Utils.getLoginUser() != null)
                    Share();
                else
                    activity.startActivity(new Intent(activity, LoginActivity.class));
                break;
        }
    }

    /**
     * 分享
     */
    private void Share() {
        HashMap<String, String> map = new HashMap<>();
        map.put("game_id", currItem.game_id);
        HttpCom.POST(khandler, HttpCom.getGameShareURL(), map, false);
    }

    private SharedDialog dialig;
    /**
     * 获取分享内容
     */
    Handler khandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ShareInfo bena = HttpUtils.DNSShare(msg.obj.toString());
                    if (bena != null) {
                        if (dialig == null) {
                            dialig = new SharedDialog(activity, bena, 2);
                            dialig.show();
                        } else if (dialig != null && !dialig.isShowing()) {
                            dialig.show();
                        }
                    } else {
                        ToastUtil.showToast("分享失败");
                    }
                    break;
                case 2:

                    break;
                default:
                    break;
            }
        }
    };

}
